<?php
/***************************************************************************
 *                                 admin.php
 *                            -------------------
 *                       (APU) Automated Pilot Utility
 *   begin                : Friday, Dec 17, 2004
 *   copyright            : (C) 2004 SF Games
 *   email                : techsupport@sf-games.com
 *
 *   Expects the following POST variables:
 *
 *            Admin ID: admin_id
 *            Password: password
 ***************************************************************************/

	$apus_root_path = '../include/';
	include($apus_root_path . 'common.php');
	include($apus_root_path . 'config.php');

	$bRedo       = false;
	$adminId     = "";
	$bAdminId    = false;
	$adminPass   = "";
	$bAdminPass  = false;

	// Get todays date. Will use year to determine years to fill into drop down controls
	$today    = getdate();
	$year     = $today["year"];
    $numYears = $year - $APU_START_YEAR;
    $totYears = $APU_START_YEAR + $numYears;

	//
	// Verify admin name and password
	//

	// Admin username
	if ( isset($_POST['admin_id']) )
	{
		if ( strlen($_POST['admin_id']) > 0 && strlen($_POST['admin_id']) <= $MAX_ID_LENGTH )
		{
			$adminId = trim($_POST['admin_id']);
			$bAdminId = true;
		}
		else
		{
			$sErrorsArray[] = "Your id is empty or exceeds the allowed length of " .$MAX_ID_LENGTH ."<br>";
			$bRedo = true;
		}
	}

	// Admin password
	if ( isset($_POST['password']) )
	{
		if ( strlen($_POST['password']) > 0 && strlen($_POST['password']) <= $MAX_PASSWORD_LENGTH )
		{
			$adminPass = trim($_POST['password']);
			$bAdminPass = true;
		}
		else
		{
			$sErrorsArray[] = "Your password is empty or exceeds the allowed length of " .$MAX_PASSWORD_LENGTH ."<br>";
			$bRedo = true;
		}
	}

	// Verify the login name
	if ( strcmp($adminId, $masterId) != 0 )
	{
		$sErrorsArray[] = "You have given an invalid administrator ID<br>";
		$bRedo = true;
	}

	// Verify the password
	if ( strcmp($adminPass, $masterPass) != 0 )
	{
		$sErrorsArray[] = "You have given an invalid administrator password<br>";
		$bRedo = true;
	}

	//
	// On bad entry, give link back to the Admin login form
	//

	if ( $bRedo == true )
	{
		//
		// Show the errors
		//

		echo("<p align=\"center\"><font color=\"#990000\" size=\"4\" face=\"Tahoma\"><b>There are errors in your login!</b></font></p>");

		echo("<div align=\"center\">");
          echo("<table width=\"480\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
            echo("<tr>");
			  echo("<ul>");
			    echo("<td width=\"491\" height=\"227\">");

		foreach ($sErrorsArray as $i => $value)
		{
			  echo("<li>");
			  echo($sErrorsArray[$i]);
			  echo("</li>");
		}

			    echo("</td>");
              echo("</ul>");
            echo("</tr>");
          echo("</table>");
        echo("</div>");
        echo("<div align=\"center\"><a href=\"adminlogin.htm\">Return to the Admin login page</a></div><br>");
	}
	else
	{
		//
		// Connect to database
		//

		$db = mysql_connect($dbhost, $dbuname, $dbpass)
			or die("Could not connect to the SQL server<br>");
		mysql_select_db($dbname);

		//
		// Spit out the page
		//

		echo("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
		echo("<html>\n");
		echo("<head>\n");
		echo("<title>APU - Admin Main</title>\n");
		echo("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n");
		echo("</head>\n");
		echo("<body bgcolor=\"#FFFFFF\" text=\"#000000\">\n");
		echo("<div align=\"center\">\n");
		  echo("<p><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"4\" color=\"#840000\"><b><font size=\"5\">APU - Automated Pilot Utility</font></b></font><br>\n");
			echo("<b><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"4\" color=\"#840000\">Administration Access</font></b></p>\n");
		  echo("<table width=\"416\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" height=\"97\">\n");
			echo("<tr>\n");
			  echo("<td width=\"416\" height=\"28\" valign=\"top\">\n");
				echo("<div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"5\" color=\"#840000\"><b><font size=\"4\">Edit Pilot</font></b></font></div>\n");
			  echo("</td>\n");
			echo("</tr>\n");
			echo("<tr>\n");
			  echo("<td valign=\"top\" height=\"100\" bgcolor=\"#FFFFCC\">\n");
				echo("<form method=\"POST\" action=\"edit_pilot.php\">\n");
				  echo("<center>\n");
					echo("<table border=\"0\" cellspacing=\"0\"width=\"100%\" height=\"61\">\n");
					  echo("<tr>\n");
						echo("<td width=\"36%\" height=\"43\"><p align=\"right\"><font color=\"#990000\"><b>Pilot ID: </b></font></p></td>\n");
						echo("<td width=\"25%\" height=\"43\"><p align=\"center\">\n");
							echo("<select name=\"pilot_id\" size=\"1\">\n");

							//
							// Fill drop down with PILOT ID's and names, sorted by ID
							//

							$query  = 'SELECT * FROM " .$table_prefix ."PILOTS ORDER BY PILOT_ID';
							$result = mysql_query($query);
							if ( !$result )
								die("Query for Pilots in table PILOTS failed.<br>");

							// For each pilot, add a drop down option
							while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
							{
							  echo("<option value=\"" .$row["PILOT_ID"] ."\">" .$row["PILOT_ID"] ." " .$row["FIRST_NAME"] ." " .$row["LAST_NAME"] ."</option>\n");
							}

							echo("</select>\n");
						  echo("</p>\n");
						echo("</td>\n");
						echo("<td align=\"center\" width=\"39%\" height=\"43\"><div align=\"left\"><font color=\"#990000\"> <b></b></font></div></td>\n");
					  echo("</tr>\n");
					  echo("<p>\n");
					echo("</table>\n");
				  echo("</center>\n");
				  echo("<div align=\"center\"><br>\n");
					echo("<input name=\"Submit1\" type=submit value=\"Submit\">\n");
				  echo("</div>\n");
				echo("</form>\n");
				echo("<p align=\"center\">&nbsp;</p>\n");
			  echo("</td>\n");
			echo("</tr>\n");
		  echo("</table>\n");

/*
		  //
		  // Edit PIREP
		  //

		  echo("<table width=\"416\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n");
			echo("<tr>\n");
			  echo("<td width=\"416\" height=\"28\" valign=\"top\">\n");
				echo("<div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"5\" color=\"#840000\"><b><font size=\"4\">Edit PIREP</font></b></font></div>\n");
			  echo("</td>\n");
			echo("</tr>\n");
			echo("<tr>\n");
			  echo("<td valign=\"top\" height=\"130\" bgcolor=\"#FFFFCC\">\n");
				echo("<form method=\"POST\" action=\"edit_pirep.php\">\n");
				  echo("<center>\n");
					echo("<table border=\"0\" cellspacing=\"0\"width=\"100%\" height=\"61\">\n");
					  echo("<tr>\n");
						echo("<td width=\"36%\" height=\"43\"><p align=\"right\"><font color=\"#990000\"><b>Pilot ID: </b></font></p></td>\n");
						echo("<td width=\"25%\" height=\"43\">\n");
						  echo("<p align=\"center\">\n");
							echo("<select name=\"pilot_id\" size=\"1\">\n");

							//
							// Fill drop down with PILOT ID's and names, sorted by ID
							//

							$query  = 'SELECT * " .$table_prefix ."FROM PILOTS ORDER BY PILOT_ID';
							$result = mysql_query($query);
							if ( !$result )
								die("Query for Pilots in table PILOTS failed.<br>");

							// For each pilot, add a drop down option
							while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
							{
							  echo("<option value=\"" .$row["PILOT_ID"] ."\">" .$row["PILOT_ID"] ." " .$row["FIRST_NAME"] ." " .$row["LAST_NAME"] ."</option>\n");
							}

							echo("</select>\n");
						  echo("</p>\n");
						echo("</td>\n");
						echo("<td align=\"center\" width=\"39%\" height=\"43\"><div align=\"left\"><font color=\"#990000\"> <b></b></font></div></td>\n");
					  echo("</tr>\n");
					  echo("<tr>\n");
						echo("<td align=\"center\" width=\"36%\" height=\"22\"><div align=\"right\"><font color=\"#990000\"><b>Month:</b></font></div></td>\n");
						echo("<td align=\"center\" width=\"25%\" height=\"22\">\n");
						  echo("<select name=\"month1\" size=\"1\">\n");
							echo("<option value=\"1\">Jan</option>\n");
							echo("<option value=\"2\">Feb</option>\n");
							echo("<option value=\"3\">Mar</option>\n");
							echo("<option value=\"4\">Apr</option>\n");
							echo("<option value=\"5\">May</option>\n");
							echo("<option value=\"6\">Jun</option>\n");
							echo("<option value=\"7\">Jul</option>\n");
							echo("<option value=\"8\">Aug</option>\n");
							echo("<option value=\"9\">Sep</option>\n");
							echo("<option value=\"10\">Oct</option>\n");
							echo("<option value=\"11\">Nov</option>\n");
							echo("<option value=\"12\">Dec</option>\n");
						  echo("</select>\n");
						echo("</td>\n");
						echo("<td align=\"center\" width=\"39%\" height=\"22\">&nbsp;</td>\n");
					  echo("</tr>\n");
					  echo("<tr>\n");
						echo("<td align=\"center\" width=\"36%\" height=\"22\"><div align=\"right\"><font color=\"#990000\"><b>Year:</b></font></div></td>\n");
						echo("<td align=\"center\" width=\"25%\" height=\"22\">\n");
						  echo("<select name=\"year1\" size=\"1\">\n");
						  // Fill in the years, from APU start year to the current year
						  for ( $nextYear = $APU_START_YEAR; $nextYear <= $totYears; $nextYear++ )
						  {
					  		echo("<option value=\" .$nextYear .\">" .$nextYear ."</option>\n");
						  }
						  echo("</select>\n");
						echo("</td>\n");
						echo("<td align=\"center\" width=\"39%\" height=\"22\"><div align=\"left\"><font color=\"#990000\"><b></b></font></div></td>\n");
					  echo("</tr>\n");
					  echo("<p>\n");
					echo("</table>\n");
				  echo("</center>\n");
				  echo("<div align=\"center\"><br>\n");
					echo("<input name=\"Submit2\" type=submit value=\"Submit\">\n");
				  echo("</div>\n");
				echo("</form>\n");
				echo("<p align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\" color=\"#999999\"><br></font></p>\n");
			  echo("</td>\n");
			echo("</tr>\n");
		  echo("</table>\n");
*/

		  echo("<table width=\"416\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n");
			echo("<tr>\n");
			  echo("<td width=\"416\" height=\"28\" valign=\"top\"><div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"5\" color=\"#840000\"><b><font size=\"4\">Create Monthly Report</font></b></font></div></td>\n");
			echo("</tr>\n");
			echo("<tr>\n");
			  echo("<td valign=\"top\" height=\"130\" bgcolor=\"#FFFFCC\">\n");
				echo("<form method=\"POST\" action=\"create_monthly.php\">\n");
				  echo("<center>\n");
					echo("<table border=\"0\" cellspacing=\"0\"width=\"100%\" height=\"61\">\n");
					  echo("<tr>\n");
						echo("<td width=\"36%\" height=\"43\"><p align=\"right\"><font color=\"#990000\"><b>Month:</b></font></p></td>\n");
						echo("<td width=\"25%\" height=\"43\">\n");
						  echo("<p align=\"center\">\n");
							echo("<select name=\"month2\" size=\"1\">\n");
							  echo("<option value=\"1\">Jan</option>\n");
							  echo("<option value=\"2\">Feb</option>\n");
							  echo("<option value=\"3\">Mar</option>\n");
							  echo("<option value=\"4\">Apr</option>\n");
							  echo("<option value=\"5\">May</option>\n");
							  echo("<option value=\"6\">Jun</option>\n");
							  echo("<option value=\"7\">Jul</option>\n");
							  echo("<option value=\"8\">Aug</option>\n");
							  echo("<option value=\"9\">Sep</option>\n");
							  echo("<option value=\"10\">Oct</option>\n");
							  echo("<option value=\"11\">Nov</option>\n");
							  echo("<option value=\"12\">Dec</option>\n");
							echo("</select>\n");
						  echo("</p>\n");
						echo("</td>\n");
						echo("<td align=\"center\" width=\"39%\" height=\"43\"><div align=\"left\"><font color=\"#990000\"> <b></b></font></div></td>\n");
					  echo("</tr>\n");
					  echo("<tr>\n");
						echo("<td align=\"center\" width=\"36%\" height=\"22\"><div align=\"right\"><font color=\"#990000\"><b>Year</b></font></div></td>\n");
						echo("<td align=\"center\" width=\"25%\" height=\"22\">\n");
						  echo("<select name=\"year2\" size=\"1\">\n");
						    // Fill in the years, from APU start year to the current year
						    for ( $nextYear = $APU_START_YEAR; $nextYear <= $totYears; $nextYear++ )
						    {
								echo("<option value=\" .$nextYear .\">" .$nextYear ."</option>\n");
							}
						  echo("</select>\n");
						echo("</td>\n");
						echo("<td align=\"center\" width=\"39%\" height=\"22\"><div align=\"left\"><font color=\"#990000\"><b></b></font></div></td>\n");
					  echo("</tr>\n");
					  echo("<p>\n");
					echo("</table>\n");
				  echo("</center>\n");
				  echo("<div align=\"center\"><br>\n");
					echo("<input name=\"Submit3\" type=submit value=\"Submit\">\n");
				  echo("</div>\n");
				echo("</form>\n");
				echo("<p align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\" color=\"#999999\"><br></font></p>\n");
			  echo("</td>\n");
			echo("</tr>\n");
		  echo("</table>\n");
		  echo("<table width=\"416\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n");
			echo("<tr>\n");
			  echo("<td width=\"416\" height=\"28\" valign=\"top\"><div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"5\" color=\"#840000\"><b><font size=\"4\">Create Year to Date Report</font></b></font></div></td>\n");
			echo("</tr>\n");
			echo("<tr>\n");
			  echo("<td valign=\"top\" height=\"130\" bgcolor=\"#FFFFCC\">\n");
				echo("<form method=\"POST\" action=\"create_ytd.php\">\n");
				  echo("<center>\n");
					echo("<table border=\"0\" cellspacing=\"0\"width=\"100%\" height=\"61\">\n");
					  echo("<tr>\n");
						echo("<td width=\"36%\" height=\"43\"><p align=\"right\"><font color=\"#990000\"><b>Year: </b></font></p></td>\n");
						echo("<td width=\"25%\" height=\"43\">\n");
						  echo("<p align=\"center\">\n");
							echo("<select name=\"select2\" size=\"1\">\n");
						      // Fill in the years, from APU start year to the current year
						      for ( $nextYear = $APU_START_YEAR; $nextYear <= $totYears; $nextYear++ )
						      {
							  	echo("<option value=\" .$nextYear .\">" .$nextYear ."</option>\n");
							  }
							echo("</select>\n");
						  echo("</p>\n");
						echo("</td>\n");
						echo("<td align=\"center\" width=\"39%\" height=\"43\"><div align=\"left\"><font color=\"#990000\"> <b></b></font></div></td>\n");
					  echo("</tr>\n");
					  echo("<p>\n");
					echo("</table>\n");
				  echo("</center>\n");
				  echo("<div align=\"center\"><br>\n");
					echo("<input name=\"Submit4\" type=submit value=\"Submit\">\n");
				  echo("</div>\n");
				echo("</form>\n");
				echo("<p align=\"center\">&nbsp;</p>\n");
			  echo("</td>\n");
			echo("</tr>\n");

			//
			// Pilot No Fly Grooming
			//

			echo("<tr>\n");
			  echo("<td width=\"416\" height=\"28\" valign=\"top\"><div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"5\" color=\"#840000\"><b><font size=\"4\">Groom No Fly Pilots</font></b></font></div></td>\n");
			echo("</tr>\n");
			echo("<tr>\n");
			  echo("<td valign=\"top\" height=\"82\" bgcolor=\"#FFFFCC\">\n");
				echo("<form method=\"POST\" action=\"pilot_no_fly.php\">\n");
				  echo("<center>\n");
					echo("<table border=\"0\" cellspacing=\"0\"width=\"100%\" height=\"61\">\n");
					  echo("<tr>\n");
					  echo("<p>\n");
					echo("</table>\n");
				  echo("</center>\n");
				  echo("<div align=\"center\"><br>\n");
					echo("<input name=\"Submit5\" type=submit value=\"Submit\">\n");
				  echo("</div>\n");
				echo("</form>\n");
				echo("<p align=\"left\">\n");
				echo("<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\" color=\"#000000\">\n");
				echo("Mark pilots as Inactive and or Deleted for no flights for configured number of days.<br>\n");
				echo("</font></p>\n");
			  echo("</td>\n");
			echo("</tr>\n");

			//
			// Database Backup button
			//

			echo("<tr>\n");
			  echo("<td width=\"416\" height=\"28\" valign=\"top\"><div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"5\" color=\"#840000\"><b><font size=\"4\">Backup Database</font></b></font></div></td>\n");
			echo("</tr>\n");
			echo("<tr>\n");
			  echo("<td valign=\"top\" height=\"82\" bgcolor=\"#FFFFCC\">\n");
				echo("<form method=\"POST\" action=\"backup/index.php\">\n");
				  echo("<center>\n");
					echo("<table border=\"0\" cellspacing=\"0\"width=\"100%\" height=\"61\">\n");
					  echo("<tr>\n");
					  echo("<p>\n");
					echo("</table>\n");
				  echo("</center>\n");
				  echo("<div align=\"center\"><br>\n");
					echo("<input name=\"Submit5\" type=submit value=\"Submit\">\n");
				  echo("</div>\n");
				echo("</form>\n");
				echo("<p align=\"left\">\n");
				echo("<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\" color=\"#000000\">\n");
				echo("This will take you to the database backup section.<br>\n");
				echo("After performing a backup, you must download the .sql or .gz file using an FTP program.<br><br>\n");
				echo("Using the download option insists on downloading the .SQL file as download.php, but it is a .SQL file.<br><br>\n");
				echo("If you want a .gz zipped file, select Download as GZip, which will create the file, but will fail to download.<br><br>\n");
				echo("You must also delete the backup file from the server, before attempting another backup, or the backup will not actually happen.<br>\n");
				echo("</font></p>\n");
			  echo("</td>\n");
			echo("</tr>\n");

			//
			// PHP info button
			//

			echo("<tr>\n");
			  echo("<td width=\"416\" height=\"28\" valign=\"top\"><div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"5\" color=\"#840000\"><b><font size=\"4\">Display Debug Information</font></b></font></div></td>\n");
			echo("</tr>\n");
			echo("<tr>\n");
			  echo("<td valign=\"top\" height=\"82\" bgcolor=\"#FFFFCC\">\n");
				echo("<form method=\"POST\" action=\"display_debug_info.php\">\n");
				  echo("<center>\n");
					echo("<table border=\"0\" cellspacing=\"0\"width=\"100%\" height=\"61\">\n");
					  echo("<tr>\n");
					  echo("<p>\n");
					echo("</table>\n");
				  echo("</center>\n");
				  echo("<div align=\"center\"><br>\n");
					echo("<input name=\"Submit5\" type=submit value=\"Submit\">\n");
				  echo("</div>\n");
				echo("</form>\n");
				echo("<p align=\"center\">&nbsp;</p>\n");
			  echo("</td>\n");
			echo("</tr>\n");

		  echo("</table>\n");
		  echo("<p><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\" color=\"#999999\">" .$APU_COPYRIGHT ."</font></p>\n");
		echo("</div>\n");
		echo("</body>\n");
		echo("</html>\n");
	}
?>
